# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if not head:
            return
        mid_list = []
        while head:
            mid_list.append(head)
            head = head.next
        mid_list[0].next, i = None, 1
        while i < len(mid_list):
            mid_list[-i].next = mid_list[-i-1]
            i += 1
        return mid_list[-1]
